package com.gopay.common.cps.send.impl;

import java.net.InetSocketAddress;

import javax.annotation.Resource;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.future.WriteFuture;
import org.apache.mina.core.service.IoConnector;

import com.gopay.common.cps.send.SendTransInfo;
import com.gopay.common.domain.acps.TranInfoBean;
import com.gopay.common.util.JsonUtils;

/**
 * 发送入账消息实现
 * 
 * @SendTransInfoImpl.java
 * @author yaobo
 * @2013-5-20 下午02:57:20 www.gopay.com.cn Inc.All rights reserved.
 */
public class SendTransInfoImpl implements SendTransInfo {
    /**
     * 服务端地址
     */
    protected String ip;

    /**
     * 端口
     */
    protected int port;

    /**
     * 发送系统
     */
    protected String from;

    @Resource(name = "nioSocketConnector")
    private IoConnector nioSocketConnector;

    @Override
    public void sendTransInfo(TranInfoBean transInfo) {
        try {
            ConnectFuture future = nioSocketConnector.connect(new InetSocketAddress(ip, port));
            // 同步调用
            future.awaitUninterruptibly();
            WriteFuture  writeFuture = future.getSession().write(JsonUtils.toJson(transInfo));
            writeFuture.awaitUninterruptibly();
            future.getSession().close(true);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public void setFrom(String from) {
        this.from = from;
    }
}
